package com.example.myapplication01.cp;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyContentProvider extends ContentProvider {

    private List<Map<String, Object>> data;

    public MyContentProvider() {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public String getType(Uri uri) {
        UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        final int URI_CODE_a = 1;
        final int URI_CODE_b = 2;
        uriMatcher.addURI("com.example.myapplication01", "person/1", URI_CODE_a);
        uriMatcher.addURI("com.example.myapplication01", "person/2", URI_CODE_b);
        switch (uriMatcher.match(uri)) {
            // 如果根据URI匹配的返回码是URI_CODE_a，则返回ContentProvider中的名为tableNameUser1的表
            case URI_CODE_a: return "person1";
            case URI_CODE_b: return "person2";
            default: return "";
        }
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values, @Nullable Bundle extras) {
        Map<String, Object> map = new HashMap<>(1);
        if (values != null) {
            Long id = values.getAsLong("id");
            String name = values.getAsString("name");
            Integer age = values.getAsInteger("age");
            map.put("id", id);
            map.put("name", name);
            map.put("age", age);
            data.add(map);
            map.clear();
        }
        if (extras != null) {
            long id = extras.getLong("id");
            String name = extras.getString("name");
            int age = extras.getInt("age");
            map.put("id", id);
            map.put("name", name);
            map.put("age", age);
            data.add(map);
        }
        return uri;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO: Implement this to handle requests to insert a new row.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public boolean onCreate() {
        // TODO: Implement this to initialize your content provider on startup.
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        // TODO: Implement this to handle query requests from clients.


        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}